package com.wgchao.identityrightsmanagement.domain.organization.model;

import com.wgchao.identityrightsmanagement.domain.IdentifiedDomainObject;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectDisabledException;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectNotExistException;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectNotMatchException;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.util.StringUtils;

/**
 * @ClassName: Account
 * @author: wgchao
 * @createTime: 2020/6/6 8:53 AM
 * @Description: 账户
 */
@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class Account extends IdentifiedDomainObject {

    //唯一 账户名
    private String username;

    //密码
    private String password;

    //机构编号
    private String agencyCode;

    //部门编号
    private String departmentCode;

    //账户名称
    private String name;

    //联系电话
    private String contactNumber;

    //邮箱
    private String email;

    //描述/备注
    private String description;

    //账户类型
    private AccountType accountType;

    //禁用标志
    private boolean disabled;

    public Account(Long id, String username, String password, String agencyCode, String departmentCode, String name,
                   String contactNumber, String email, String description, int accountType, Long version) {
        this.setId(id);
        this.username = username;
        this.password = password;
        this.agencyCode = agencyCode;
        this.departmentCode = departmentCode;
        this.name = name;
        this.contactNumber = contactNumber;
        this.email = email;
        this.description = description;
        this.accountType = AccountType.getByIndex(accountType);
        this.setVersion(version);
    }

    //注册管理员账户
    public static Account registerAccount(AccountCreator accountCreator, Agency agency){
        if(!agency.normal()){
            throw new ObjectDisabledException("机构不可用");
        }
        if(AccountType.getByIndex(accountCreator.getAccountType()) == null){
            throw new ObjectNotMatchException("账户类型错误");
        }
        return new Account(accountCreator.getUsername(), accountCreator.getPassword(), agency.getAgencyCode(), null,
                accountCreator.getName(), accountCreator.getContactNumber(), accountCreator.getEmail(),
                accountCreator.getDescription(), AccountType.getByIndex(accountCreator.getAccountType()), false);
    }

    //修改账户基本信息
    public void modifyAccountInfo(AccountUpdator accountUpdator, Agency agency){
        if(!agency.normal()){
            throw new ObjectDisabledException("机构不可用");
        }
        if(accountUpdator.getName() != null){
            this.name = accountUpdator.getName().orElse(null);
        }
        if(accountUpdator.getContactNumber() != null){
            this.contactNumber = accountUpdator.getContactNumber().orElse(null);
        }
        if(accountUpdator.getEmail() != null){
            this.email = accountUpdator.getEmail().orElse(null);
        }
        if(accountUpdator.getName() != null){
            this.description = accountUpdator.getDescription().orElse(null);
        }
    }

    //状态是否正常
    public boolean normal(){
        return !this.disabled;
    }

    public void normalException(){
        if(!this.normal()){
            throw new ObjectDisabledException(this.username+"账户不可用");
        }
    }

    //禁用
    public void disable(){
        this.disabled = true;
    }
    //恢复使用
    public void regain(){
        this.disabled = false;
    }

    //设置账户所在部门
    public void setDepartmentCode(String departmentCode){
        this.departmentCode = departmentCode;
    }

    public void ascriptionDepartment(){
        if(StringUtils.isEmpty(this.departmentCode)){
            throw new ObjectNotExistException("账户没有分配部门");
        }
    }
}
